<template>
  <div class='container'>
    <el-form
      style="margin-bottom:50px"
      ref="formDataRef"
      :model="formData"
      :rules="rules"
      label-width="80px"
    >
      <el-form-item label="所属学科" size="small" prop="subjectName"  v-if="!$route.query.id">
        <el-select v-model="formData.subjectName" placeholder="请选择" style="width:100%">
          <el-option v-for="item in sbjList" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" size="small" prop="directoryName">
        <el-input v-model="formData.directoryName" placeholder="请输入目录名称"></el-input>
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button @click="hCancel">取消</el-button>
      <el-button type="primary" @click="hSubmit">确认</el-button>
    </el-row>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
import { list as getSubjectList } from '@/api/hmmm/subjects.js'
export default {
  props: {
    isEdit: {
      required: true,
      type: Boolean
    },
    backFormData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      formData: {
        subjectName: '',
        directoryName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      sbjList: []
    }
  },
  created () {
    // 获取 学科 列表
    if (this.$route.query.id) {
      this.formData.subjectName = Number(this.$route.query.id)
    } else {
      this.LoadSbjList()
    }

    if (this.isEdit) {
      this.formData = {
        ...this.backFormData
      }
    }
  },
  methods: {
    // 获取简单学科列表
    async LoadSbjList () {
      const res = await getSubjectList()
      console.log('学科列表', res)
      this.sbjList = res.data.items
    },

    // 清空表单
    hClean () {
      this.formData = {
        subjectName: '',
        directoryName: ''
      }
      this.$refs.formDataRef.resetFields()
    },

    // 点击取消
    hCancel () {
      this.$emit('close')
    },

    // 点击提交
    hSubmit () {
      this.$refs.formDataRef.validate(async voild => {
        if (voild) {
          this.isEdit ? await this.doUpdate() : await this.doAdd()
          this.$emit('success')
        }
      })
    },

    // 做添加
    async doAdd () {
      try {
        console.log(this.formData)
        const res = await add({
          ...this.formData,
          subjectID: this.formData.subjectName
        })
        if (res.status === 200) {
          this.$message.success('添加成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }
    },

    // 做修改
    async doUpdate () {
      try {
        const res = await update({
          ...this.formData,
          subjectID: this.formData.subjectName
        })
        if (res.status === 200) {
          this.$message.success('修改成功')
        }
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  }
}
</script>

<style scoped lang='less'></style>
